<?php

function load_data($server_ip, $server_port, $server_pass, $server_fail) {


    $socket = fsockopen($server_ip, $server_port); //Connects to the SHOUTCast server
    
    if (!$socket) { echo $server_fail; exit; } //Unable to connect to the server

	$xml = '';	
    $info = "GET /admin.cgi?pass=$server_pass&mode=viewxml&sid=1 HTTP/1.0\r\n"; //Pull Server Stats
    $info .= "User-Agent: Mozilla\r\n"; //User agent (required to connect)
	$info .="Icy-MetaData:1\r\n\r\n";
    
	fputs($socket, $info);
    while (!feof($socket)) { $xml .= fgets($socket, 1000); } //Puts all server stat information into $xml
	
    $headers = array("SONGTITLE", "SONGURL", "IRC", "ICQ", "AIM", "WEBHITS", "STREAMHITS", "STREAMSTATUS", "BITRATE", "CONTENT", "VERSION", "WEBDATA", "LISTENERS", "SONGHISTORY", "CURRENTLISTENERS", "PEAKLISTENERS", "MAXLISTENERS", "REPORTEDLISTENERS", "AVERAGETIME", "SERVERGENRE", "SERVERURL", "SERVERTITLE"); //Various Elements
    $stats = array(); //Create array that will be used to access the stats
    foreach ($headers as $element) {
        $elementlength = strlen("<$element>"); //Gets the length of the element
        $pos1 = strpos($xml, "<$element>") + $elementlength; //Determines starting position of the statistic
        $pos2 = strpos($xml, "</$element>"); //Determines ending position of the statistic
        $length = $pos2 - $pos1; //Determines the length of the statistic
        $value = substr($xml, $pos1, $length); //Using the above information, it pulls the statistic.
        $stats[$element] = $value; //Adds statistic to the array
    }

    //This work is licensed under the Creative Commons Attribution-No Derivative Works 2.5 Canada License.
    //To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/2.5/ca/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
    //Usage: Linking back to voscast.com is not required, but is appreciated. You are not permitted to re-distribute modified versions of the script, unless permission has been given. For support, contact support@voscast.com.

    $listenerelements = array("HOSTNAME", "USERAGENT", "UNDERRUNS", "CONNECTTIME", "POINTER", "UID"); //elements for LISTENERS
    $listeners = array(); //Creates array for LISTENERS

    $countelements = substr_count($stats['LISTENERS'], "<LISTENER>"); //Counts how many times <LISTENER> is in the array...
    $countnumber = 1; //We use this to create array numbers

    while ($countnumber <= $countelements) { //Continue as long as the number is not greater then the amount of times <LISTENER> was found
        $elementlength = strlen("<LISTENER>"); //Gets the length of the element
        $pos1 = strpos($stats['LISTENERS'], "<LISTENER>") + $elementlength; //Determines starting position of the statistic
        $pos2 = strpos($stats['LISTENERS'], "</LISTENER>"); //Determines ending position of the statistic
        $length = $pos2 - $pos1; //Determines the length of the statistic
        $value = substr($stats['LISTENERS'], $pos1, $length); //Using the above information, it pulls the statistic.

        foreach ($listenerelements as $listenerelementv) {
            $elementlength = strlen("<$listenerelementv>"); //Gets the length of the element
            $pos21 = strpos($value, "<$listenerelementv>") + $elementlength; //Determines starting position of the statistic
            $pos22 = strpos($value, "</$listenerelementv>"); //Determines ending position of the statistic
            $length2 = $pos22 - $pos21; //Determines the length of the statistic
            $value2 = substr($value, $pos21, $length2); //Using the above information, it pulls the statistic.
            $listeners[$countnumber][$listenerelementv] = $value2; //Adds statistic to the array
        }

        $replace = "<LISTENER>$value</LISTENER>"; //What we wish to replace (remove)
        $stats['LISTENERS'] = str_replace($replace, "", $stats['LISTENERS']); //Removes the value
        $countnumber = $countnumber + 1; //Increase the count number
    }

    $songhistoryelements = array("PLAYEDAT", "TITLE"); //elements for SONGHISTORY
    $songhistory = array(); //Creates array for SONGHISTORY

    $countelements = substr_count($stats['SONGHISTORY'], "<SONG>"); //Counts how many times <SONG> is in the array...
    $countnumber2 = 1; //We use this to create array numbers

    while ($countnumber2 <= $countelements) { //Continue as long as the number is not greater then the amount of times <LISTENER> was found
        $elementlength = strlen("<SONG>"); //Gets the length of the element
        $pos1 = strpos($stats['SONGHISTORY'], "<SONG>") + $elementlength; //Determines starting position of the statistic
        $pos2 = strpos($stats['SONGHISTORY'], "</SONG>"); //Determines ending position of the statistic
        $length = $pos2 - $pos1; //Determines the length of the statistic
        $value1 = substr($stats['SONGHISTORY'], $pos1, $length); //Using the above information, it pulls the statistic.

        foreach ($songhistoryelements as $songhistoryelementv) {
            $elementlength = strlen("<$songhistoryelementv>"); //Gets the length of the element
            $pos31 = strpos($value1, "<$songhistoryelementv>") + $elementlength; //Determines starting position of the statistic
            $pos32 = strpos($value1, "</$songhistoryelementv>"); //Determines ending position of the statistic
            $length3 = $pos32 - $pos31; //Determines the length of the statistic
            $value3 = substr($value1, $pos31, $length3); //Using the above information, it pulls the statistic.
            $songhistory[$countnumber2][$songhistoryelementv] = $value3; //Adds statistic to the array
        }

        $stats['SONGHISTORY'] = preg_replace("/<SONG>/", "", $stats['SONGHISTORY'], 1); //Removes the value
        $stats['SONGHISTORY'] = preg_replace("/<\/SONG>/", "", $stats['SONGHISTORY'], 1); //Removes the value
        $countnumber2 = $countnumber2 + 1; //Increase the count number
    }


    $webdataelements = array("INDEX", "LISTEN", "PALM7", "LOGIN", "LOGINFAIL", "PLAYED", "COOKIE", "ADMIN", "UPDINFO", "KICKSRC", "KICKDST", "UNBANDST", "BANDST", "VIEWBAN", "UNRIPDST", "RIPDST", "VIEWRIP", "VIEWXML", "VIEWLOG", "INVALID"); //elements for WEBDATA
    $webdata = array(); //Creates array for WEBDATA

    foreach ($webdataelements as $webdataelementv) {
        $elementlength = strlen("<$webdataelementv>"); //Gets the length of the element
        $pos1 = strpos($stats['WEBDATA'], "<$webdataelementv>") + $elementlength; //Determines starting position of the statistic
        $pos2 = strpos($stats['WEBDATA'], "</$webdataelementv>"); //Determines ending position of the statistic
        $length = $pos2 - $pos1; //Determines the length of the statistic
        $value = substr($stats['WEBDATA'], $pos1, $length); //Using the above information, it pulls the statistic.
        $webdata[$webdataelementv] = $value; //Adds statistic to the array
    }

    fclose($socket);
    return $stats;

}

function replace_special_characters($string)
{
	$string = preg_replace("/&#?[a-z0-9]{2,8};/i","",$string);
	return $string;
}

?>